package com.bd.wechat.service.impl;


import com.bd.wechat.core.LoginMember;
import com.bd.wechat.entity.Member;
import com.bd.wechat.mapper.MemberMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private MemberMapper memberMapper;


    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        Member member = memberMapper.selectMemberByAccount(account);
        if (member == null) {
            throw new UsernameNotFoundException("member not found");
        }

        return createLoginMember(member);
    }

    public UserDetails createLoginMember(Member member) {
        LoginMember loginMember = new LoginMember();
        loginMember.setMemberId(member.getId());
        loginMember.setAccount(member.getAccount());
        loginMember.setMember(member);

        return loginMember;
    }

}
